Passed
Push — develop ( e1aa15...9c5965 )
by Xaver
01:05
created

map.js ➔ ... ➔ mapActiveArea   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
dl 0
loc 9
rs 10
nop 0
1
define(['map/clientlayer', 'map/labellayer', 'map/button', 'leaflet', 'map/activearea'],
2
  function (ClientLayer, LabelLayer, Button, L) {
3
    'use strict';
4
5
    var options = {
6
      worldCopyJump: true,
7
      zoomControl: true,
8
      minZoom: 0
9
    };
10
11
    return function (linkScale, sidebar, buttons) {
12
      var self = this;
13
      var savedView;
14
15
      var map;
16
      var layerControl;
17
      var baseLayers = {};
18
19
      function saveView() {
20
        savedView = {
21
          center: map.getCenter(),
22
          zoom: map.getZoom()
23
        };
24
      }
25
26
      function contextMenuOpenLayerMenu() {
27
        document.querySelector('.leaflet-control-layers').classList.add('leaflet-control-layers-expanded');
28
      }
29
30
      function mapActiveArea() {
31
        map.setActiveArea({
32
          position: 'absolute',
33
          left: sidebar.getWidth() + 'px',
34
          right: 0,
35
          top: 0,
36
          bottom: 0
37
        });
38
      }
39
40
      function setActiveArea() {
41
        setTimeout(mapActiveArea, 300);
42
      }
43
44
      var el = document.createElement('div');
45
      el.classList.add('map');
46
47
      map = L.map(el, options);
48
      mapActiveArea();
49
50
      var now = new Date();
51
      config.mapLayers.forEach(function (item, i) {
52
        if ((typeof item.config.start === 'number' && item.config.start <= now.getHours()) || (typeof item.config.end === 'number' && item.config.end > now.getHours())) {
53
          item.config.order = item.config.start * -1;
54
        } else {
55
          item.config.order = i;
56
        }
57
      });
58
59
      config.mapLayers = config.mapLayers.sort(function (a, b) {
60
        return a.config.order - b.config.order;
61
      });
62
63
      var layers = config.mapLayers.map(function (d) {
64
        return {
65
          'name': d.name,
66
          'layer': L.tileLayer(d.url.replace('{retina}', L.Browser.retina ? '@2x' : ''), d.config)
67
        };
68
      });
69
70
      map.addLayer(layers[0].layer);
71
72
      layers.forEach(function (d) {
73
        baseLayers[d.name] = d.layer;
74
      });
75
76
      var button = new Button(map, buttons);
77
78
      map.on('locationfound', button.locationFound);
79
      map.on('locationerror', button.locationError);
80
      map.on('dragend', saveView);
81
      map.on('contextmenu', contextMenuOpenLayerMenu);
82
83
      if (config.geo) {
84
        [].forEach.call(config.geo, function (geo) {
85
          L.geoJSON(geo.json, geo.option).addTo(map);
86
        });
87
      }
88
89
      button.init();
90
91
      layerControl = L.control.layers(baseLayers, [], { position: 'bottomright' });
92
      layerControl.addTo(map);
93
94
      map.zoomControl.setPosition('topright');
95
96
      var clientLayer = new ClientLayer({ minZoom: config.clientZoom });
97
      clientLayer.addTo(map);
98
      clientLayer.setZIndex(5);
99
100
      var labelLayer = new LabelLayer({ minZoom: config.labelZoom });
101
      labelLayer.addTo(map);
102
      labelLayer.setZIndex(6);
103
104
      sidebar.button.addEventListener('visibility', setActiveArea);
105
106
      map.on('zoom', function () {
107
        clientLayer.redraw();
108
        labelLayer.redraw();
109
      });
110
111
      map.on('baselayerchange', function (e) {
112
        map.options.maxZoom = e.layer.options.maxZoom;
113
        clientLayer.options.maxZoom = map.options.maxZoom;
114
        labelLayer.options.maxZoom = map.options.maxZoom;
115
        if (map.getZoom() > map.options.maxZoom) {
116
          map.setZoom(map.options.maxZoom);
117
        }
118
119
        var style = document.querySelector('.css-mode:not([media="not"])');
120
        if (style && e.layer.options.mode !== '' && !style.classList.contains(e.layer.options.mode)) {
121
          style.media = 'not';
122
          labelLayer.updateLayer();
123
        }
124
        if (e.layer.options.mode) {
125
          var newStyle = document.querySelector('.css-mode.' + e.layer.options.mode);
126
          newStyle.media = '';
127
          newStyle.appendChild(document.createTextNode(''));
128
          labelLayer.updateLayer();
129
        }
130
      });
131
132
      map.on('load', function () {
133
        var inputs = document.querySelectorAll('.leaflet-control-layers-selector');
134
        [].forEach.call(inputs, function (input) {
135
          input.setAttribute('role', 'radiogroup');
136
          input.setAttribute('aria-label', input.nextSibling.innerHTML.trim());
137
        });
138
      });
139
140
      var nodeDict = {};
141
      var linkDict = {};
142
      var highlight;
143
144
      function resetMarkerStyles(nodes, links) {
145
        Object.keys(nodes).forEach(function (d) {
146
          nodes[d].resetStyle();
147
        });
148
149
        Object.keys(links).forEach(function (d) {
150
          links[d].resetStyle();
151
        });
152
      }
153
154
      function setView(bounds, zoom) {
155
        map.fitBounds(bounds, { maxZoom: (zoom ? zoom : config.nodeZoom) });
156
      }
157
158
      function goto(m) {
159
        var bounds;
160
161
        if ('getBounds' in m) {
162
          bounds = m.getBounds();
163
        } else {
164
          bounds = L.latLngBounds([m.getLatLng()]);
165
        }
166
167
        setView(bounds);
168
169
        return m;
170
      }
171
172
      function updateView(nopanzoom) {
173
        resetMarkerStyles(nodeDict, linkDict);
174
        var m;
175
176
        if (highlight !== undefined) {
177
          if (highlight.type === 'node' && nodeDict[highlight.o.node_id]) {
178
            m = nodeDict[highlight.o.node_id];
179
            m.setStyle(config.map.highlightNode);
180
          } else if (highlight.type === 'link' && linkDict[highlight.o.id]) {
181
            m = linkDict[highlight.o.id];
182
            m.setStyle(config.map.highlightLink);
183
          }
184
        }
185
186
        if (!nopanzoom) {
187
          if (m) {
188
            goto(m);
189
          } else if (savedView) {
190
            map.setView(savedView.center, savedView.zoom);
191
          } else {
192
            setView(config.fixedCenter);
193
          }
194
        }
195
      }
196
197
      self.setData = function setData(data) {
198
        nodeDict = {};
199
        linkDict = {};
200
201
        clientLayer.setData(data);
202
        labelLayer.setData(data, map, nodeDict, linkDict, linkScale);
203
204
        updateView(true);
205
      };
206
207
      self.resetView = function resetView() {
208
        button.disableTracking();
209
        highlight = undefined;
210
        updateView();
211
      };
212
213
      self.gotoNode = function gotoNode(d) {
214
        button.disableTracking();
215
        highlight = { type: 'node', o: d };
216
        updateView();
217
      };
218
219
      self.gotoLink = function gotoLink(d) {
220
        button.disableTracking();
221
        highlight = { type: 'link', o: d[0] };
222
        updateView();
223
      };
224
225
      self.gotoLocation = function gotoLocation(d) {
226
        button.disableTracking();
227
        map.setView([d.lat, d.lng], d.zoom);
228
      };
229
230
      self.destroy = function destroy() {
231
        button.clearButtons();
232
        sidebar.button.removeEventListener('visibility', setActiveArea);
233
        map.remove();
234
235
        if (el.parentNode) {
236
          el.parentNode.removeChild(el);
237
        }
238
      };
239
240
      self.render = function render(d) {
241
        d.appendChild(el);
242
        map.invalidateSize();
243
      };
244
245
      return self;
246
    };
247
  });
248